// pages/project_mycharge/project_mycharge.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectList: [],
    sessionID: "",
    loginFlag: "0",
    userInfo: "",
    chuangNum: "",
  },
  // 跳转到“我参加的”
  gotoMyjoin(e) {
    wx.navigateTo({
      url: "/pages/project/project",
    })
  },

  // 跳转到“简历”
  gotoApplication(e) {
    wx.navigateTo({
      url: "/pages/project/application/application",
    })
  },

  // 用于携带POST请求需要的参数
  Parameters: {
    pageParam: {
      orderBy: "id desc",
      pageNum: 1,
      pageSize: 10,
    },
    teamType: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 取缓存
    this.setData({
      userInfo:
        wx.getStorageSync(
          "userInfo"
        ), // 用户的微信头像、昵称等信息
      loginFlag:
        wx.getStorageSync(
          "loginFlag"
        ), // 判断是否登录，登录为1，未登录为0或空
      sessionID:
        wx.getStorageSync(
          "sessionID"
        ),
      chuangNum:
        wx.getStorageSync(
          "chuangNum"
        ),
    })
    console.log(
      this.data.loginFlag
    )
  },

  // wx.showModal 和 wx.login封装
  myShowModal() {
    wx.showModal({
      title: "提示",
      content:
        "登录后查看更多，是否登录？",
      success: res => {
        // 确定
        if (res.confirm) {
          this.logIn()
        }
        // 取消
        else {
          wx.switchTab({
            url: "/pages/notice/notice",
          })
        }
      },
    })
  },

  // 登录
  logIn() {
    // 如果userInfo为空，则未确认过微信授权弹窗，即首次登录
    if (!this.data.userInfo) {
      // 微信授权弹窗
      wx.getUserProfile({
        desc: "用于获取用户头像和昵称",
        // 用户点击确认
        success: res => {
          let userInfo =
            res.userInfo
          wx.setStorageSync(
            "userInfo",
            userInfo
          ) // 缓存userInfo
          this.myLogin() // 进行登录
        },
        // 用户点击取消
        fail: res => {
          wx.showToast({
            title:
              "跳转到首页",
            icon: "none",
            duration: 1500,
          })
        },
      })
    }
    // 非首次登录
    else {
      this.myLogin() // 重新登录
    }
  },

  // wx.login()封装
  myLogin() {
    wx.login({
      success: res => {
        wx.request({
          url:
            "https://www.hschuangsai.com/user/login/" +
            res.code,
          method: "GET",
          success: res => {
            console.log(
              "myLogin success",
              res
            )
            let sessionID =
              res.data.data
                .sessionId
            wx.setStorageSync(
              "sessionID",
              sessionID
            ) // 缓存sessionID
            wx.setStorageSync(
              "loginFlag",
              1
            ) // 缓存loginFlag
            wx.setStorageSync(
              "chuangNum",
              res.data.data
                .chuangNum
            )
            this.setData({
              sessionID:
                sessionID,
              loginFlag: 1,
            })
            wx.showToast({
              title:
                "成功登录啦",
              icon: "none",
              duration: 1500,
            })
          },
        })
      },
    })
  },

  getproject() {
    if (
      this.data.loginFlag == 1
    ) {
      console.log(
        "in getproject"
      )
      wx.request({
        url: "https://www.hschuangsai.com/user/team",
        header: {
          "content-type":
            "application/json",
          session:
            this.data
              .sessionID,
        },
        data: this.Parameters,
        method: "POST",
        success: result => {
          //console.log("project:",result)
          if (
            result.data
              .code == -1
          ) {
            //会话失效
            wx.showToast({
              title:
                result.data
                  .commonErrorCode
                  .errorSuggestion,
              icon: "none",
            })
          } else {
            this.setData({
              projectList:
                result.data
                  .data.list,
            })
            //console.log(result.data.data.list)
          }
        },
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (
      this.data.loginFlag == 1
    ) {
      console.log(
        "in on show"
      )
      this.getproject()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom:
    function () {},

  onTabItemTap: function () {
    // 取缓存
    this.setData({
      userInfo:
        wx.getStorageSync(
          "userInfo"
        ), // 用户的微信头像、昵称等信息
      loginFlag:
        wx.getStorageSync(
          "loginFlag"
        ), // 判断是否登录，登录为1，未登录为0或空
      sessionID:
        wx.getStorageSync(
          "sessionID"
        ),
      chuangNum:
        wx.getStorageSync(
          "chuangNum"
        ),
    })
    console.log(
      this.data.loginFlag
    )
    // 如果未登录，则显示遮罩和提示
    if (
      this.data.loginFlag != 1
    ) {
      console.log("第二次")
      this.myShowModal()
    }

    this.getproject()
  },
})
